var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

var WxParse = require('../../wxParse/wxParse.js');

var app = getApp();
var that;

//获取应用实例
var app = getApp()
Page({
  data: {
    user: {}
    ,tid:0
    ,team:null
    ,group:null
    ,sgid:0
    ,cjt:0
    ,yjt:0
    ,cjtstr:"距离比赛开始"
    ,wks:true
    ,rens:0
    ,list:{}
    ,rens:0
    ,hasgid:0
    ,sgback:0
    ,jiaru:false
    ,showr:false
    , txt: "<view class='f09 fw mar_b5' >K币领取细则</view>"
      ,zjbs:0
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onShow:function(){
    if(that.data.sgback == 1){
      that.data.sgback =0;
      that.userjoin();
    }
  },
  onPullDownRefresh:function(){
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:false
    });
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    
    if( !util.judge.isNull(options.tid) ){
      that.data.tid = parseInt(options.tid);
    }

    if( !util.judge.isNull(options.scene) ){
      var scene = decodeURIComponent(options.scene)
      var arrs=scene.split("_");
      if(arrs.length >0){
        that.data.tid = arrs[0];
      }
    }

    if( !util.judge.isNull(options.zjbs) ){
        that.data.zjbs = parseInt(options.zjbs);
    }

    that.loadData();
    
  },

  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        console.log(res);
      that.setData({
        user:res
      })

      var url = config.url_get_yd_dk_txt;
      var params = {type:"team_create_remark"}
      return wxRequest.postRequest(url, params);

    })
    .then(res=>{
        if(util.judge.isObject(res)){
            var article =res.txt1;
            WxParse.wxParse('article', 'html', article, that, 0);
            WxParse.wxParse('article_js', 'html',  res.txt2, that, 0);
        }

        console.log(res);
      if(that.data.tid == 0){
        throw util.stop();
      }
      
      var url = config.url_team_user_is;
      var params = {
        id:that.data.tid
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {

        console.log(res);

      if(util.judge.isNull(res)){
      
      }else{
        wx.redirectTo({
          url: '../team/main?tid='+ that.data.tid+"&zjbs="+that.data.zjbs
        });
        
        throw util.stop();
      }

      var url = config.url_team_get;
      var params = {
          id:that.data.tid
          ,code:"all"
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
        if(util.judge.isObject(res)){

            that.data._sdesc="D几名邀请您参加\""+res.name+ "\"比赛！" ;
            wx.setNavigationBarTitle( { title: res.name });

          var bd = util.str2date(res.bdate);
          var ed = util.str2date(res.edate);

          if(res.pwd =="n"||res.pwd ==""){
              that.setData({showr:true});
          }

          res.bdate = util.date2str(res.bdate,"MM.dd");
          res.edate = util.date2str(res.edate,"MM.dd");
         
          if(!res.remark){
              res.remark="这家伙很懒什么都没有留下";
          }

          var d1 = new Date();    //结束时间  

          if(res.isclose ==1){
              that.setData({cjtstr:"比赛已关闭",wks:false,jiaru:false});
          }else{

              if(d1.getTime() > ed.getTime()){
                  that.setData({cjtstr:"比赛已结束",wks:false,jiaru:false});
              }
              else if(bd.getTime() > d1.getTime()){
                  that.setData({cjtstr:"比赛未开始",wks:false,jiaru:true});
              }
              else{
                  //进行中
                  var date3 = ed.getTime() - d1.getTime();   //时间差的毫秒数 
                  var days=parseFloat(date3/(24*3600*1000));  
                  if(days < 1){
                      that.setData({cjtstr:"比赛今天结束",wks:false,jiaru:true});
                  }else{
                      days=that.modFoat(days);
                      that.setData({ cjtstr: "距离比赛结束", wks: true,jiaru:true });
                  }
              }
          }

          date3 = ed.getTime() - bd.getTime();   //时间差的毫秒数        
          var yjt = parseInt(parseFloat(date3/(24*3600*1000) + 0.00001))+1; 
          that.setData({cjt:days,yjt:yjt,team:res});
        }
    })
    .then(res=>{

      var url = config.url_team_group;
      var params = {
        id:that.data.tid
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);

        if(util.judge.isArray(res)){
          if(res.length >0){
            that.data.hasgid=1;
          }
          
          that.setData({group:res});
        }
    })
    .then(res=>{ //获取已经参赛人的 前5位
      var url = config.url_team_user;
      var params = {
        id: that.data.tid
        ,pageIndex:1
        ,pageIndex:20
      }
      return wxRequest.postRequest2(url, params)
    })
    .then(res => {
        console.log(res);
        if(util.judge.isObject(res)){
          that.setData({list:res.data,rens:res.count});
        }
        
    })


    .catch(res => {
        console.log(res);
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading()
        wx.stopPullDownRefresh();
    });
    
    
  },
  radioChange: function(e) {
    console.log(e.detail.value);
    that.data.sgid = e.detail.value;
  },
  btnJoin:function(){
    //判断团是否有组,有组就显示,没有就直接加入吧
    // if(that.data.group.length>0){
    //   if(that.data.sgid ==0){
    //     util.alert("请选择要加入组");
    //     return;
    //   }
    // }

      if ( that.data.team.pwd =="" || that.data.team.pwd =="n")
      {
        that.userjoin();
      }
      else{
          wx.navigateTo({
            url: '../team/selectgroup'
          })
      }
    
    // if(that.data.hasgid == 0){
    //   else{
    //     wx.navigateTo({
    //       url: '../team/selectgroup'
    //     })
    //   }

    // }
  },
  userjoin:function(){
   
    util.showLoading();

    var url = config.url_team_user_in;
    var params = {
      id:that.data.tid,
      gid :that.data.sgid,
      hasgid:that.data.hasgid
    }

    wxRequest.postRequest(url, params)
    .then(res => {
      if(res==1){
        util.alert("加入成功",function(){
          wx.redirectTo({
            url: '../team/main?tid='+ that.data.tid
          });
        });
      }else if(res==22){
        util.alert("请勿重复加入竞赛");
      }
    })
    .catch(res => {
        util.alert(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });

  }
    , modFoat:function(v) {
    var _max = parseInt(v) + 1;
    if( _max - v < 1 ) {
        return _max;
    }
    return v;
} 

})